package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.Menu;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface MenuMapper {
    @Select("select * from t_menu where linkUrl is null")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "linkUrl",property = "linkUrl"),
            @Result(column = "path",property = "path"),
            @Result(column = "priority",property = "priority"),
            @Result(column = "description",property = "description"),
            @Result(column = "icon",property = "icon"),
            @Result(column = "id",property = "children",javaType = List.class,many = @Many(select = "com.itheima.dao.MenuMapper.findMenuByMenuId")),

    })
    List<Menu> findMenu();

    @Select("select * from t_menu where parentMenuId=#{id}")
    List<Menu> findMenuByMenuId(Integer id);

    @Select("select * from t_menu")
    Page<Menu> findPage();

    @Select("SELECT tr.`keyword` FROM t_role_menu trm,t_role tr WHERE  trm.`menu_id`=#{id} AND trm.`role_id`=tr.`id`")
    List<String> findKeywordList(Integer id);

    @Insert("INSERT INTO t_menu (NAME,parentMenuId,linkUrl,priority,icon,disable) values (#{name},#{parentMenuId},#{linkUrl},#{priority},#{icon},#{disable})")
    void addmenu(Menu menu);

    @Select("select * from t_menu where id = #{id}")
    Menu findMenuByid(Integer id);


    /*
     UPDATE t_menu
    <set>
       <if test="name != null">
       name = #{name},
       </if>
       <if test="linkUrl != null">
       linkUrl = #{linkUrl},
       </if>
       <if test="path != null">
       path = #{path},
       </if>
       <if test="priority != null">
       priority = #{priority},
       </if>
       <if test="icon != null">
       icon = #{icon},
       </if>
       <if test="description != null">
       description = #{description},
       </if>
       <if test="parentMenuId != null">
       parentMenuId = #{parentMenuId},
       </if>
       <if test="level != null">
       level = #{level},
       </if>
       <if test="disable != null">
       disable = #{disable},
       </if>
    </set>
     where id = #{id}
     */
    @Update("<script>UPDATE t_menu\n" +
            "    <set>\n" +
            "       <if test=\"name != null\">\n" +
            "       name = #{name},\n" +
            "       </if>\n" +
            "       <if test=\"linkUrl != null\">\n" +
            "       linkUrl = #{linkUrl},\n" +
            "       </if>\n" +
            "       <if test=\"path != null\">\n" +
            "       path = #{path},\n" +
            "       </if>\n" +
            "       <if test=\"priority != null\">\n" +
            "       priority = #{priority},\n" +
            "       </if>\n" +
            "       <if test=\"icon != null\">\n" +
            "       icon = #{icon},\n" +
            "       </if>\n" +
            "       <if test=\"description != null\">\n" +
            "       description = #{description},\n" +
            "       </if>\n" +
            "       <if test=\"parentMenuId != null\">\n" +
            "       parentMenuId = #{parentMenuId},\n" +
            "       </if>\n" +
            "       <if test=\"level != null\">\n" +
            "       level = #{level},\n" +
            "       </if>\n" +
            "       <if test=\"disable != null\">\n" +
            "       disable = #{disable},\n" +
            "       </if>\n" +
            "    </set>\n" +
            "     where id = #{id}</script>")
    void editMapper(Menu menu);

    @Delete("DELETE  FROM t_menu WHERE id = #{id}")
    void delete(Integer id);
}
